import os
import sys
import json
import cocr.imagedeal as ideal

import cv2
from  cocr.predict.tagPredict import predict

from flask import Flask,request, redirect, url_for


UPLOAD_FOLDER = '/data/imgs/resize/'
SRC_FOLDER='/data/imgs/src'
ALLOWED_EXTENSIONS = set(['jpg', 'jpeg', 'gif'])
app = Flask(__name__)
tagpredict=predict()
app.config['UPLOAD_FOLDER'] = "/data/imgs"
app.config['ALLOWED_EXTENSIONS'] = ALLOWED_EXTENSIONS

def getImageTag():
    if request.method=='POST':
        file=request.files['file']
        gray=getGrayImage(file)
        sex=tagpredict.detect(gray,'sex')
        age=tagpredict.detect(gray,'age')
        expression=tagpredict.detect(gray,'expression');
        v={};
        v['sex']=sex;
        v['age']=age;
        v['expression']=expression;
        json.dump(v)
        return v
def getGrayImage(img):
    if img.ndim == 3:
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    else:
        gray = img